package com.education.welco.controller;

import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.TSchooldept;
import com.education.welco.service.ITClassService;
import com.education.welco.service.ITSchooldeptService;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 学校部门 信息操作处理
 *
 * @author d
 * @date 2020-05-12
 */
@RestController
@RequestMapping("/tSchooldept")
public class TSchooldeptController {

    @Autowired
    private ITSchooldeptService tSchooldeptService;

    @Autowired
    private ITClassService classService;
    /**
     *  查出最后一级的系，以及下面的班级
     */


    /**
     * 格式化创建时间
     */
    private List<TSchooldept> formatTime(List<TSchooldept> list) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (TSchooldept schoolDept : list) {
            String format = simpleDateFormat.format(schoolDept.getCreateTime());
            schoolDept.setCreateTimeString(format);
        }
        return list;
    }

    /**
     * 递归集合    根据第一级的id查询出下级列表
     */
    public List<TSchooldept> listAll(List<TSchooldept> list) {
        for (TSchooldept schoolDept : list) {
            //根据第一级的id查询出下级列表
            List<TSchooldept> sDeptS = tSchooldeptService.selectTSchooldeptByPrentId(schoolDept.getId());
            if (sDeptS != null && sDeptS.size() > 0) {
                //格式化创建时间  返回时间被格式化过的集合
                List<TSchooldept> tSchoolDeptS = formatTime(sDeptS);
                //设置子级集合
                schoolDept.setChildren(listAll(tSchoolDeptS));
            }
//            List<TSchooldept> tSchoolDeptS = formatTime(sDeptS);

            //最后一级 设置班级集合


        }
        return list;
    }

//    /**
//     *  递归集合    根据第一级的id查询出班级列表
//     */
//    public List<TSchooldept> listClass(List<TSchooldept> list){
//        for (TSchooldept schoolDept : list) {
//            //根据第一级的id查询出下级列表
//            List<TSchooldept> sDeptS = tSchooldeptService.selectTSchooldeptByPrentId(schoolDept.getId());
//            for (TSchooldept sDept : sDeptS) {
//                Integer id = sDept.getId();
//                List<TClass> tClasses = classService.selectTClassAllByDeptId(id);
//                sDept.setChildrenClass(tClasses);
//            }
//
//        }
//        return list;
//    }

    /**
     * 递归集合    根据第一级的id查询出下级列表
     */
    public List<TSchooldept> listAllOther(List<TSchooldept> list) {
        for (TSchooldept schoolDept : list) {
            //根据第一级的id查询出下级列表
            List<TSchooldept> sDeptS = tSchooldeptService.selectTSchooldeptByPrentIdOther(schoolDept.getId());
            if (sDeptS != null && sDeptS.size() > 0) {
                //格式化创建时间  返回时间被格式化过的集合
//                List<TSchooldept> tSchoolDeptS = formatTime(sDeptS);
                //设置子级集合
                schoolDept.setChildren(listAllOther(sDeptS));
            }
        }
        return list;
    }


    /**
     * 查询学校部门列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(TSchooldept tSchooldept, Integer pageNum, Integer pageSize) {
        if (tSchooldept.getSchoolId() != null) {
            if (pageNum != null && pageSize != null) {
                PageHelper.startPage(pageNum, pageSize);
            }
            tSchooldept.setParentId(0);
            //查出部门的第一级列表
            List<TSchooldept> list = tSchooldeptService.selectTSchooldeptList(tSchooldept);
            //对集合中的时间进行格式化
            List<TSchooldept> schoolDept = formatTime(list);
            //查出所有的部门集合
            List<TSchooldept> schoolDeptList = listAll(schoolDept);
//            List<TSchooldept> tSchooldepts = listClass(schoolDeptList);

            //PageInfo<TSchooldept> pageInfo = new PageInfo<>(schoolDeptList);
            return new ApiReturnObject("200", "查询成功", schoolDeptList);
//            return new ApiReturnObject("200","查询成功",tSchooldepts);
        }
        return new ApiReturnObject("201", "缺少schoolId", "");
    }

    /**
     * 查询学校部门列表
     */
    @GetMapping("/listOther")
    public ApiReturnObject listOther(TSchooldept tSchooldept, Integer pageNum, Integer pageSize) {
        if (tSchooldept.getSchoolId() != null) {
            tSchooldept.setParentId(0);
            //查出部门的第一级列表
            List<TSchooldept> list = tSchooldeptService.selectTSchooldeptListOther(tSchooldept);
            //对集合中的时间进行格式化
//            List<TSchooldept> schoolDept = formatTime(list);
            //查出所有的部门集合
            List<TSchooldept> schoolDeptList = listAllOther(list);
            return new ApiReturnObject("200", "查询成功", schoolDeptList);
        }
        return new ApiReturnObject("201", "缺少schoolId", "");
    }


    /**
     * 添加 用户时查询部门列表 选取最后一级 供进行选择
     */
    @GetMapping("/deptList")
    public ApiReturnObject deptList(Integer schoolId) {
        if (schoolId != null) {
            ArrayList<TSchooldept> arrayList = Lists.newArrayList();
            List<TSchooldept> schooldeptList = tSchooldeptService.selectTSchooldeptByPrentId2(schoolId);
            for (TSchooldept tSchooldept : schooldeptList) {
                List<TSchooldept> tSchooldepts = tSchooldeptService.selectTSchooldeptByPrentId(tSchooldept.getValue());
                if (tSchooldepts != null && tSchooldepts.size() == 0) arrayList.add(tSchooldept);
            }
            return new ApiReturnObject("200", "查询成功", arrayList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /**
     * 查询列表-不分级
     */
    @GetMapping("/listAll")
    public ApiReturnObject listAll(TSchooldept schooldept) {
        if (schooldept.getSchoolId() != null) {
            List<TSchooldept> schooldeptList = tSchooldeptService.selectTSchooldeptListAll(schooldept);
            return new ApiReturnObject("200", "查询成功", schooldeptList);
        }
        return new ApiReturnObject("201", "缺少schoolId", "");

    }

    /**
     * 判断现在要添加的组织架构是第几级
     *
     * @param parentId
     * @param i
     * @return
     */
    public Integer judgeLevel(Integer parentId, int i) {
        TSchooldept schooldept = tSchooldeptService.selectTSchooldeptById(parentId);
        if (schooldept != null) {
            i = i + 1;
            i = judgeLevel(schooldept.getParentId(), i);
        }
        return i;
    }

    /**
     * 新增组织架构
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody TSchooldept tSchooldept) {
        //判断组织架构层级 最多三级
        int i = 0;
        Integer integer = judgeLevel(tSchooldept.getParentId(), i);
        if (integer > 2) {
            return new ApiReturnObject("201", "组织架构最多三级", "");
        }
        tSchooldept.setCreateTime(new Date());
        tSchooldeptService.insertTSchooldept(tSchooldept);
        return new ApiReturnObject("200", "添加成功", "");
    }


    /**
     * 修改保存学校部门
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody TSchooldept tSchooldept) {
        tSchooldept.setUpdateTime(new Date());
        tSchooldeptService.updateTSchooldept(tSchooldept);
        return new ApiReturnObject("200", "修改成功", "");
    }

    /**
     * 删除学校部门
     */
    @GetMapping("/del")
    public ApiReturnObject remove(Integer id) {
        //查询该部门以及子部门下的班级列表
        TSchooldept schooldept = new TSchooldept();

        //再判断班级下是否存在学生  是否能够删除

        tSchooldeptService.deleteTSchooldeptById(id);
        return new ApiReturnObject("200", "删除成功", "");
    }
}